#!/bin/csh -f 
# qconvert: Master script to convert restraint files to Aqua format
# =============================================================
# Ton Rullmann, August 7, 1996
# =============================================================

# JFD adds:
set debug                           = 1

if ( $#argv == 0 ) goto usage
goto begin

#####################
# usage block
#####################
usage:
echo "usage: qconvert  [options]  restraint_file  [restraint_type]"
echo ""
echo "   options (can be in any order):"
echo "   [-r restraint_prefix]   [-od output_dir]  [-l]  [-fatal]  [-simple]" 
echo "   [-t file_type]  [-mr|-nomr]  [-i]  [-nosplit]  [-h value]  [-nosavtyp]"
echo "   [-f]  [-c value] "  
echo ""
echo "   -r       : file name (minus extension) of converted restraint files"
echo "              if not given, the name is derived from the input file"
echo "   -od      : directory where the split and the converted files will be put"
echo "              if not given, the current directory is used"
echo "   -l       : show qconvr output"
echo "   -fatal   : qmodr exits immediately upon error instead of continuing"
echo "   -simple  : qmodr not used; all data treated as NOE type"
echo "   -t  1    : DISGEO"
echo "       2    : XPLOR"
echo "       3    : BIOSYM"
echo "       4    : DIANA/DISMAN"
echo "      99    : user defined"
echo "              if not given, the file_type will be determined automatically"
echo "   -(no)mr  : defines whether input file is (not) an MR file"
echo "              if not given, the MR type will be determined automatically"
echo "   -i       : identifiers START_OF... are already present in file;"
echo "              data types will not be interpreted"
echo "   -nosplit : guess file type, but skip splitting, and start conversion"
echo "              directly, assuming that split files already exist"
echo "   -h value : value of 'how' input variable in qmodr"
echo "   -nosavtyp: set 'nosavtyp' input variable in qmodr to 1"
echo "   -f       : turn on 'fixorig' option in qconvr (only relevant for MR)"
echo "   -c value : value of 'chain' input variable in qconvr (only relevant for MR)"
echo ""
echo "   restraint type: one of:  noe, hb, ss, dis, tor"
echo "   If restraint type not given all available types will be handled"
exit

#####################
# initialization
#####################
begin:

# these names should correspond to the names of the files produced by qsplitr
set inext_noe  = "raw_noe"
set inext_hb   = "raw_hb"
set inext_ss   = "raw_ss"
set inext_dis  = "raw_dis"
set inext_tor  = "raw_tor"
set inext_mdl  = "raw_mdl"

# these names are the Aqua default
set outext_noe = "noe"
set outext_hb  = "hb"
set outext_ss  = "ss"
set outext_dis = "dis"
set outext_tor = "tor"

# mr flag:   -1: determine MR file signature;   0: is not MR;   1: is MR;
# mr_seek is used as parameter for qguessr and qmodr (best to always initialize as -1)
# mr is the parameter passed to qconvr
set mr_seek    = -1
set mr         = -1
set ide        = 1
set list       = 0
set filtyp     = 0
set fatal      = 0
set outdir     = "."
set todo       = ""
set typ        = ""
set mrfile     = ""
set restrname  = ""
set fixorig    = 0
set howval     = 0
set nosavtyp   = 0
set chnval     = ""
set final_status = 0

#####################
# process the options
#####################
options:
switch ( "$1" )
    case "-l":
	set list = 1
	shift
	breaksw
    case "-mr":
	set mr = 1
	shift
	breaksw
    case "-nomr":
	set mr = 0
	shift
	breaksw
    case "-fatal":
	set fatal = 1
	shift
	breaksw
    case "-simple":
	set todo = "simple"
	shift
	breaksw
    case "-od":
	if ( $#argv < 1 ) goto usage
	shift
	set outdir = $1
	if ( ! -d $outdir ) then
	    echo "* Output directory" $outdir "does not exist"
	    exit( 1 )
	endif
	shift
	breaksw
    case "-t":
	if ( $#argv < 1 ) goto usage
	shift
	set filtyp = $1
	shift
	breaksw
    case "-n":
	echo "-n option obsolete; use -r option instead"
	exit( 1 )
    case "-r":
        if ( $#argv < 1 ) goto usage
        shift
        set restrname = $1
        shift
        breaksw
    case "-i":
	set ide = 0
	shift
	breaksw
    case "-f":
	set fixorig = 1
	shift
	breaksw
    case "-nosavtyp":
	set nosavtyp = 1
	shift
	breaksw
    case "-h":
	if ( $#argv < 1 ) goto usage
	shift
	set howval = $1
	shift
	breaksw
    case "-c":
	if ( $#argv < 1 ) goto usage
	shift
	set chnval = $1
	shift
	breaksw
    case "-nosplit":
	set todo = "nosplit"
	shift
	breaksw
    default:
	goto endoptions
endsw
goto options

#####################
# set restraint file
#####################
endoptions:
if ( $#argv < 1 ) goto usage
set restrfile = $1
if ( ! -e $restrfile ) then
    echo "* Restraint file "$restrfile" does not exist"
    exit( 1 )
endif
shift

#####################
# prepare for Aqua
#####################
source $aquaroot/aqsetup
if ( $restrname == "" ) set restrname = `qgetproj $restrfile`
echo "* AQUA scripts directory:" $AQUASCRIPTSDIR 
echo "* Restraint input file is" $restrfile
echo "* Restraint output file names begin with" $restrname

#####################
# guess and split
#####################
if ( ! $filtyp  || $mr_seek == -1 ) then
    echo " "
    echo "* Starting qguessr"
# look for MR file signature, if mr_seek not 0 (best value: always seek) 
    qguessr mr=$mr_seek filtyp=$filtyp $restrfile
    set sts = $status
# MR file signature; do not override value of mr given on input
    if ( $sts >= 100 ) then 
	@ sts = $sts - 100
	if ( $mr == -1 ) set mr = 1
	if ( $mr_seek == -1 ) set mr_seek = 1
    else
	if ( $mr == -1 ) set mr = 0
	if ( $mr_seek == -1 ) set mr_seek = 0
    endif
# file type; do not override value given on input
    if ( ! $filtyp ) then
	if ( $sts == 0 ) then
	    echo "* ERROR: could not determine file type"
	    exit( 1 )
	else
	    set filtyp = $sts
	endif
    endif
endif
echo "filtyp" $filtyp
echo "mr" $mr
if ( $mr ) then
    set mrfile = $outdir/$restrname.$inext_mdl
else
    set mrfile = ""
endif


if ( -e qmodr.log ) \rm qmodr.log
if ( $todo == "nosplit" ) then
    goto again
endif
if ( $todo == "simple" ) then
    echo "START_OF_NOE_DATA"                                            >  $restrname.restr.tmp 
    qmodr -v filtyp=$filtyp -v mr=$mr_seek $restrfile                   >> $restrname.restr.tmp 
else
    echo " "
    echo "* Starting qmodr"
    qmodr -v filtyp=$filtyp -v ide=$ide -v cmm=1 -v mr=$mr_seek -v fatal=$fatal \
	-v how=$howval -v nosavtyp=$nosavtyp $restrfile                 > $restrname.restr.tmp 
    set sts = $status
    if ( $sts || -e qmodr.log ) then
	echo "* Classifying restraints in qmodr: WARNING or ERROR messages:"
	cat qmodr.log
	\rm qmodr.log
	if ( $sts ) exit( 1 )
    endif
endif

echo " "
echo "* Starting qsplitr to split the file"
qsplitr -v ident=$outdir/$restrname $restrname.restr.tmp
\rm $restrname.restr.tmp
echo "Preparing for qconvr"

#####################
# start loop over restraint types
#####################
again:

if ( $#argv == 1 ) then
# process only one restraint type, given by remaining argument
    set typ = $1
else
# process all types one by one
    switch ( $typ )
	case "noe":
	    set typ = "hb"
	    breaksw
	case "hb":
	    set typ = "ss"
	    breaksw
	case "ss":
	    set typ = "dis"
	    breaksw
	case "dis":
	    set typ = "tor"
	    breaksw
	case "tor":
	    \rm qconvr.out >& /dev/null
	    exit ( $final_status )
	default:
	    set typ = "noe"
	    breaksw
    endsw
endif

# set the file extensions
switch ( $typ )
    case "noe":
	set inext  = $inext_noe
	set outext = $outext_noe
	breaksw
    case "hb":
	set inext  = $inext_hb
	set outext = $outext_hb
	breaksw
    case "ss":
	set inext  = $inext_ss
	set outext = $outext_ss
	breaksw
    case "dis":
	set inext  = $inext_dis
	set outext = $outext_dis
	breaksw
    case "tor":
	set inext  = $inext_tor
	set outext = $outext_tor
	breaksw
    default:
        echo "Code bug in qconvert. Found unknown type: $typ"
	exit ( $final_status )
endsw

if ( $typ == "tor" ) then

# process tor type
# ================

    set file = $outdir/$restrname.$inext
    set outfile = $outdir/$restrname.$outext
    if ( -e $file ) then
	echo " "
        echo "* Converting <"$file"> to <"$outfile">"
	if ( $mr ) echo "* IGNORING mrfile (and possible options)"
	qconvr -b $file << EOD > qconvr.out
2
$filtyp
$outfile
QCONVR
EOD

##         echo "Status: " $status
##         if ( $debug ) then 
##             echo "Before status"
##         endif
        
	set sts = $status
	if ( $list || $sts ) then
	    echo "* Output from qconvr:"
	    cat qconvr.out
	    echo "* End of output from qconvr"
	endif
	if ( $sts ) then
	    set final_status = 1
	else
	    echo "* Conversion successful"
	endif
    else
#	echo file $file does not exist
	\rm -f $outfile
    endif

else

# process noe, hb, ss or dis type
# ===============================

    set file = $outdir/$restrname.$inext
    set outfile = $outdir/$restrname.$outext
    set opt = "-b"
    if ( -e $file ) then
	if ( $mr ) then
	    echo " "
            echo "* Converting <"$file"> with mrfile <"$mrfile">"
            echo "* to <"$outfile">"
	    if ( $chnval != "" ) then
		echo "* using 'chain' value" $chnval
		set opt = ( $opt "-c" $chnval )
	    endif
	    if ( $fixorig ) then
		echo "* using 'fixorig' option"
		set opt = ( $opt "-f" )
	    endif
	else
	    echo " "
            echo "* Converting <"$file"> without mrfile to <"$outfile">"
	endif
        
	qconvr $opt $file $mrfile << EOD > qconvr.out
1
$filtyp
$typ
$outfile
QCONVR conversion $typ distance restraints from $restrfile
EOD

	set sts = $status
	if ( $list || $sts ) then
	    echo "* Output from qconvr:"
	    cat qconvr.out
	    echo "* End of output from qconvr"
	else
	    if ( $mr ) cat convMRTABLE.log
	endif
	if ( $sts ) then
	    set final_status = 1
	else
	    echo "* Conversion successful"
	endif
	if ( $mr ) \rm convMRTABLE.log
    else
#	echo file $file does not exist
	\rm -f $outfile
    endif


endif

# process next type, if required
if ( $#argv < 1 ) goto again
\rm qconvr.out >& /dev/null
